package com.zhangch.base.hutool;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import org.junit.Test;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.toList;

/**
 * 功能说明: <br>
 * 系统版本: 1.0 <br>
 * 开发人员: zhanch
 * 开发时间: 2021/3/9<br>
 * <br>
 */
public class MdataAndHrDbTest {

	public static void main(String[] args) {

	}

	@Test
	public void queryTest() throws SQLException {
		List<Entity> hrFind = Db.use("mdata").query("SELECT\n" +
				"\tperson_code \n" +
				"FROM\n" +
				"\tmdata_hr_person_attr \n" +
				"WHERE\n" +
				"\tattr_type = 'state' \n" +
				"\tAND attr_val = '0' \n" +
				"\tAND start_date < now() AND end_date > now()");
		List<String> perSoncdes = hrFind.stream().map(entity -> (String) entity.get("person_code")).collect(toList());

		List<Entity> imsFind = Db.use("hr").query("select state,account from sso_account where account in ("+StrUtil.join(",",perSoncdes)+")");
		List<String> accounts = new ArrayList<>();
		imsFind.forEach(entity ->{
			String state = (String) entity.get("state");
			if(state != null && "Y".equals(state)){
				accounts.add((String) entity.get("account"));
			}
		});
		List<String> Imsaccounts = new ArrayList<>();
		CollUtil.split(accounts,1000).forEach(list ->{
			List<Entity> _imsFind = null;
			try {
				list = list.stream().map(str -> "\'"+str+"\'").collect(Collectors.toList());

				//imsFind = Db.use("ims").query("select emp_state from SMSUSER.b_salesemp_info where emp_code in ('hzongm01')");
				_imsFind = Db.use("ims").query("select emp_code,emp_state from SMSUSER.b_salesemp_info where emp_code in ("+StrUtil.join(",",list)+")");
			} catch (SQLException throwables) {
				throwables.printStackTrace();
			}
			Map<String, Entity> imsMap = _imsFind.stream()
					.collect(Collectors.toMap(entiy -> (String) entiy.get("emp_code"), Function.identity()));
			imsMap.keySet().forEach(emp_code ->{
				Entity entity = imsMap.get(emp_code);
				if(((String)entity.get("emp_state")).equals("Y")){
					Imsaccounts.add(emp_code);
				}
			});

		});


		System.out.println(CollUtil.join(accounts,","));

		System.out.println("-------------");
		System.out.println(CollUtil.join(Imsaccounts,","));

	}

	private boolean noEqual(Entity entity, Entity _entity) {
		String state = (String) entity.get("state");
		String emp_state = (String) _entity.get("emp_state");
		return (!state.equals(emp_state) && state.equals("Y") && emp_state.equals("N"))
				|| (!state.equals(emp_state) && state.equals("D") && emp_state.equals("Y"));
	}

	private String part_1(){
		StringBuilder stringBuilder = new StringBuilder();
		IntStream.range(0, 1000).forEach(index ->{
			if(index == 99){
				stringBuilder.append("?");
			}else {
				stringBuilder.append("?,");
			}
		});
		return stringBuilder.toString();
	}
}
